<?php

namespace App\Kuafu\Wechat\Service;

use App\Kuafu\Shop\Service\ShopAppletService;
use App\Kuafu\Wechat\Enum\CodeEnum;
use App\Kuafu\Wechat\Enum\WechatCodeEnum;
use App\Kuafu\Wechat\Exceptions\WechatException;
use App\Library\FeiShu\SendFs;
use App\Library\SnowFlake\SnowFlake;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Kuafu\Base\Service\BaseService;
use GuzzleHttp\Client as GuzzleClient;
use App\Kuafu\Order\Enum\CodeEnum as OrderCodeEnum;

class WechatMiniProgramService extends BaseService
{
    /**
     * 图片融合用, 生成小程序二维码, 扫码进入小程序,领取图片下载, 有效期 x 天
     *
     * @param string $imgUrl
     * @param int $expiredDays
     * @return string
     * @throws GuzzleException
     * @throws WechatException
     */
    public static function genImageFusion(string $imgUrl, int $expiredDays = 30): string
    {
        $key = 'ImageKey_' . SnowFlake::nextIdCache();
        Redis::SET($key, $imgUrl, 'EX', $expiredDays * 24 * 60 * 60);

        $data = [
            'scene' => $key,
            "page" => "pages/share/share"
        ];
        return WechatService::createQrCode(OrderCodeEnum::QR_CODE_THREE, $data);
    }

    /**
     * 根据返回获取图片
     *
     * @param string $key
     * @return mixed
     */
    public static function getImageBySceneKey(string $key)
    {
        return Redis::GET($key);
    }
}
